﻿using System;
using System.Collections.Generic;
using DevelopTool.Bll.PowerPlanBlls;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.PowerPlanModels;
using Sunny.UI;

namespace DevelopTool.Forms.SystemToolForms
{
    /// <summary>
    /// 电源计划
    /// </summary>
    public partial class PowerPlanForm : BaseUIPage
    {
        private System.Windows.Forms.Timer timer;


        public PowerPlanForm()
        {
            InitializeComponent();
        }

        private void PowerPlanForm_Load(object sender, EventArgs e)
        {
            this.Disposed += this.PowerPlanForm_Disposed;

            // 获取系统电源计划列表
            List<Powercfg> powercfgs = PowerPlanBll.GetPowercfgList();
            powercfgs.Insert(0, new Powercfg() { UUID = "0", Name = "无", IsActive = false });

            //绑定默认电源计划下拉框数据
            uiComboBoxDefaultModelValue.ValueMember = "UUID";
            uiComboBoxDefaultModelValue.DisplayMember = "Name";
            uiComboBoxDefaultModelValue.DataSource = powercfgs;
            uiComboBoxDefaultModelValue.SelectedValue = Setting.PowerPlan.DefaultModelPlan;

            //绑定离开模式下拉框数据
            uiComboBoxLeaveModeValue.ValueMember = "UUID";
            uiComboBoxLeaveModeValue.DisplayMember = "Name";
            uiComboBoxLeaveModeValue.DataSource = powercfgs;
            uiComboBoxLeaveModeValue.SelectedValue = Setting.PowerPlan.LeaveModePlan;

            //绑定加速模式下拉框数据
            uiComboBoxSpeedupModeValue.ValueMember = "UUID";
            uiComboBoxSpeedupModeValue.DisplayMember = "Name";
            uiComboBoxSpeedupModeValue.DataSource = powercfgs;
            uiComboBoxSpeedupModeValue.SelectedValue = Setting.PowerPlan.SpeedupModePlan;

            upDownIdleTimeoutValue.Value = Setting.PowerPlan.IdleTimeout;
            upDownCPUUsageValue.Value = Setting.PowerPlan.CPUUsage;


            #region 定时任务
            this.timer = new System.Windows.Forms.Timer
            {
                Interval = 500,
                Enabled = true//启动计时器
            };
            this.timer.Tick += delegate (object ss, EventArgs ee)
            {
                this.lblCpuUsage.Text = WindowsHelper.CPUTotalUsage + "%";
                this.lblIdleTimeSeconds.Text = WindowsHelper.GetIdleTimeSeconds() + "秒";

                ShowLog(PowerPlanLogBll.GetLog());
            };
            #endregion 定时任务
        }

        


        /// <summary>
        /// 显示日志
        /// </summary>
        /// <param name="log"></param>
        private void ShowLog(LogModel log)
        {
            if (log == null) return;
            if (string.IsNullOrEmpty(log.LogStr)) return;
            string logStr = $"[{log.OperTime.String()}] {log.LogStr}{Environment.NewLine}";
            this.codeEditorLog.ContentText = this.codeEditorLog.ContentText.Insert(0, logStr);            
        }


        /// <summary>
        /// 默认电源计划 选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiComboBoxDefaultModelValue_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (uiComboBoxDefaultModelValue.ContainsFocus)
            {
                Setting.PowerPlan.DefaultModelPlan = uiComboBoxDefaultModelValue.SelectedValue.ToString();
                ShowLog(new LogModel(Model.Enums.LogType.Info, $"设定[默认]电源计划为【{uiComboBoxDefaultModelValue.SelectedText}】"));
            }
        }


        /// <summary>
        /// 键鼠空闲超过（秒） 选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void upDownIdleTimeoutValue_ValueChanged(object sender, int value)
        {
            Setting.PowerPlan.IdleTimeout = upDownIdleTimeoutValue.Value;
        }


        /// <summary>
        /// 离开模式 选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiComboBoxLeaveModeValue_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (uiComboBoxLeaveModeValue.ContainsFocus)
            {
                Setting.PowerPlan.LeaveModePlan = uiComboBoxLeaveModeValue.SelectedValue?.ToString();
                ShowLog(new LogModel(Model.Enums.LogType.Info, $"设定[离开模式]电源计划为【{uiComboBoxLeaveModeValue.SelectedText}】"));
            }
        }

        /// <summary>
        /// CPU使用率大于（百分比） 选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void upDownCPUUsageValue_ValueChanged(object sender, int value)
        {
            Setting.PowerPlan.CPUUsage = upDownCPUUsageValue.Value;
        }

        /// <summary>
        /// 加速模式 选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiComboBoxSpeedupModeValue_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (uiComboBoxSpeedupModeValue.ContainsFocus)
            {
                Setting.PowerPlan.SpeedupModePlan = uiComboBoxSpeedupModeValue.SelectedValue.ToString();
                ShowLog(new LogModel(Model.Enums.LogType.Info, $"设定[加速模式]电源计划为【{uiComboBoxSpeedupModeValue.SelectedText}】"));
            }
        }


        private void PowerPlanForm_Disposed(object sender,EventArgs e)
        {
            if(this.timer != null)
            {
                this.timer.Stop();
                this.timer.Dispose();
            }
        }
    }
}
